package cn.gson.school.controller.jiuye;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.ImagesDao;
import cn.gson.school.dao.jiuye.InterviewRecordEntityDao;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.jiuye.FileEntity;
import cn.gson.school.entity.jiuye.InterviewExperienceEntity;
import cn.gson.school.entity.jiuye.InterviewMaterialsEntity;
import cn.gson.school.entity.jiuye.InterviewRecordEntity;
import cn.gson.school.entity.jiuye.MultimediaEntity;
import cn.gson.school.entity.jiuye.OfferEntity;
import cn.gson.school.entity.jiuye.RetestEntity;
import cn.gson.school.entity.xitong.ImageEntity;
import cn.gson.school.mappers.jiuye.FileEntityMapper;
import cn.gson.school.mappers.jiuye.InterviewMaterialsEntityMapper;
import cn.gson.school.mappers.jiuye.InterviewRecordEntityMapper;
import cn.gson.school.mappers.jiuye.MultimediaEntityMapper;
import cn.gson.school.mappers.jiuye.OfferEntityMapper;
import cn.gson.school.mappers.jiuye.RetestEntityMapper;

@Controller
@RequestMapping("/jingli")
public class InterviewRecordController {
	@Autowired
	private InterviewRecordEntityDao interviewRecordEntityDao;
	@Autowired
	private InterviewRecordEntityMapper interviewRecordEntityMapper;
	@Autowired
	private InterviewMaterialsEntityMapper interviewMaterialsEntityMapper;
	@Autowired
	private MultimediaEntityMapper multimediaEntityMapperv;
	@Autowired
	private RetestEntityMapper retestEntityMapper;
	@Autowired
	private OfferEntityMapper offerEntityMapper;
	@Autowired
	private FileEntityMapper fileEntityMapper;
	@Autowired
	private ImagesDao  imagesDao;
	@Value("${upload.dir}")
	private String Upload;
	
	@RequestMapping("jingli")
	@ResponseBody
	public Map<String, Object> index(InterviewRecordEntity interviewRecordEntity,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<InterviewRecordEntity> listDate=interviewRecordEntityMapper.list(interviewRecordEntity);
		PageInfo<InterviewRecordEntity> info=new PageInfo<>(listDate);
		Map<String,Object> obj=new HashMap<String,Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	@GetMapping({ "jy-ms-jinglixiangqing", "jy-ms-jinglixiangqing/{id}"})
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			System.out.println("id:"+id);
			InterviewRecordEntity u = interviewRecordEntityDao.findOne(id);
			System.out.println("u:"+u);
			if (u != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				StudentEntity e=u.getStudentinfoId();
				Long Interviewid=u.getInterviewId();
				//查询所有的资料信息
				List<InterviewMaterialsEntity> materials=interviewMaterialsEntityMapper.chaxunziliao(Interviewid);
				Map<String, Object> multimedia=multimediaEntityMapperv.chaxunduomeiti(Interviewid);
				//查询复试
				ImageEntity yin = null;
				if(multimedia!=null){
					yin=imagesDao.findOne((Long) multimedia.get("touxiang"));
					model.addAttribute("yinpath",Upload+yin.getImagePath());
					model.addAttribute("yin", yin);
				}
				List<RetestEntity> retest=retestEntityMapper.chaxunfushi(Interviewid);
				Map<String,Object> offer=offerEntityMapper.chaxunoffer(Interviewid);
				ImageEntity img=null;
				System.out.println(offer);
				if(offer!=null) {
					Long offerid=(Long) offer.get("offer_id");
					Map<String,Object> file=fileEntityMapper.chaxunguidang(offerid);
					model.addAttribute("file",file);
					Long imageid=(Long) offer.get("touxiang");
					img=imagesDao.findOne(imageid);
					model.addAttribute("imgpath","upload/"+img.getImagePath());
					model.addAttribute("img",img);
				}
				model.addAttribute("xinxi", u);
				model.addAttribute("xueshen",e);
				model.addAttribute("materials",materials);
				model.addAttribute("multimedia",multimedia);
				model.addAttribute("retest",retest);
				model.addAttribute("offer",offer);
			}
		}
		return "jiuye/jy-ms-jinglixiangqing";
	}
	
}
